package com.linln.admin.pcs.controller;


import com.linln.admin.system.service.AppService;
import com.linln.common.utils.HelperUtil;
import com.linln.common.utils.ValidationUtil;
import com.linln.component.actionLog.annotation.EntityParam;
import com.linln.modules.system.domain.Porter;
import com.linln.modules.system.domain.PorterCredentials;
import com.linln.modules.system.repository.PorterCredentialsRepository;
import com.linln.modules.system.repository.PorterRepository;
import com.linln.modules.system.repository.RoleRepository;
import com.linln.modules.system.repository.UserRepository;
import com.linln.modules.system.service.RoleService;
import com.linln.modules.system.service.UserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.List;


/**
 * 护工证书管理
 *
 */
@Controller
@RequestMapping("/pcs/porter/credentials")
public class PorterCredentialsController {




    @Autowired
    private AppService appService;

    @Autowired
    private PorterRepository porterRepository;
    @Autowired
    private PorterCredentialsRepository porterCredentialsRepository;


    /**
     * 列表页面
     */
    @GetMapping("/index")
    @RequiresPermissions("pcs:porterCredentials:index")
    public ResponseEntity<?> index(Model model, @EntityParam PorterCredentials porterCredentials) {

        return appService.layTable(porterCredentialsRepository, before -> {
                    model.addAttribute("porterId", porterCredentials.getPorterId());
                    // 添加模板变量
                    return model;

                },

                after -> {

                    // 添加查询条件
                    return (Specification<PorterCredentials>) (root, query, cb) -> {
                        Predicate predicate = cb.conjunction();  // 初始条件
                        Long porterId = porterCredentials.getPorterId();
                        predicate = cb.and(predicate, cb.equal(root.get("porterId"), porterId));

                        return predicate;
                    };
                },

                "/pcs/porter_credentials/index",

                (pageResult) -> {
                    // 在这里处理 pageResult
                    // 例如：修改每一条数据
                }

        );

    }


    /**
     * 跳转到添加页面
     */
    @RequestMapping("/form")
    @RequiresPermissions("pcs:porterCredentials:add")
    public ResponseEntity<?> form(Model model, @EntityParam PorterCredentials porterCredentials) throws Exception {
        return appService.form(porterCredentialsRepository, "/pcs/porter_credentials/form",
                (isEdit) -> {
                    model.addAttribute("porterId", porterCredentials.getPorterId());
                    return model;
                },
                (isEdit, vo) -> {
                    // 在这里处理 vo 变量
                    PorterCredentials data = (PorterCredentials) vo;
                    //时间格式序列化为yyyy-MM-dd 格式
                    data.setExpireDateStr(HelperUtil.dateFormat(data.getExpireDate(),"yyyy-MM-dd"));
                    model.addAttribute("vo", data);
                    // 你可以在这里添加其他逻辑
                    return model;
                },
                (data) -> {
                    // 保存前数据校验
                    List<ValidationUtil.ValidationRule<?>> rules = new ArrayList<>();
                    // 添加邮箱的校验规则
                    // rules.add(new ValidationUtil.ValidationRule<>(
                    //     porter.getCardNo(),
                    //     email -> email != null,
                    //     "Email格式不正确"
                    // ));

                    // 执行校验
                    return ValidationUtil.validateFields(rules);
                },
                (data) -> {
                    // 数据处理
                    if (porterCredentials.getPorterId() != null) {
                        Porter origin = porterRepository.getById(porterCredentials.getPorterId());
                        porterCredentials.setCompanyId(origin.getCompanyId());
                    }

                    return porterCredentials;
                },
                (data) -> {
                    // 保存后续逻辑在这写
                });
    }


    /**
     * 修改状态
     * @param model
     * @param porterCredentials
     * @return
     * @throws Exception
     */
    @RequestMapping("/state")
    @RequiresPermissions("pcs:porterCredentials:state")
    public ResponseEntity<?> state(Model model, @EntityParam PorterCredentials porterCredentials) throws Exception {

        return appService.save(porterCredentialsRepository, (data) -> {

            // 校验数据
            return ValidationUtil.validateFields(new ArrayList<>());

        }, (data) -> {
            // 处理数据
            PorterCredentials origin = porterCredentialsRepository.getById(porterCredentials.getId());
            origin.setStatus(porterCredentials.getStatus());

            return origin;

        }, (data) -> {
            // 更新后操作
        });

    }


}
